#Boa:Frame:Frame_Agendamento
# -*- coding: utf-8 -*-

import wx
import wx.grid
import wx.lib.masked.timectrl
import wx.lib.masked.textctrl
import wx.calendar
import time
import datetime


def create(parent):
    return Frame_Agendamento(parent)

[wxID_FRAME_AGENDAMENTO, wxID_FRAME_AGENDAMENTOBUTTON1, 
 wxID_FRAME_AGENDAMENTOBUTTON2, wxID_FRAME_AGENDAMENTOBUTTON3, 
 wxID_FRAME_AGENDAMENTOBUTTON4, wxID_FRAME_AGENDAMENTOBUTTON5, 
 wxID_FRAME_AGENDAMENTOBUTTON6, wxID_FRAME_AGENDAMENTOCALENDARCTRL1, 
 wxID_FRAME_AGENDAMENTOCHECKBOX1, wxID_FRAME_AGENDAMENTOCRISTINASSTATICBOX, 
 wxID_FRAME_AGENDAMENTODATEPICKERCTRL1, wxID_FRAME_AGENDAMENTOLISTBOX1, 
 wxID_FRAME_AGENDAMENTOLISTBOX3, wxID_FRAME_AGENDAMENTOSTATICBOX1, 
 wxID_FRAME_AGENDAMENTOSTATICTEXT1, wxID_FRAME_AGENDAMENTOSTATICTEXT2, 
 wxID_FRAME_AGENDAMENTOSTATICTEXT3, wxID_FRAME_AGENDAMENTOSTATICTEXT4, 
 wxID_FRAME_AGENDAMENTOSTATICTEXT5, wxID_FRAME_AGENDAMENTOSTATICTEXT6, 
 wxID_FRAME_AGENDAMENTOSTATICTEXT7, wxID_FRAME_AGENDAMENTOTEXTCTRL1, 
 wxID_FRAME_AGENDAMENTOTEXTCTRL2, wxID_FRAME_AGENDAMENTOTEXTCTRL3, 
 wxID_FRAME_AGENDAMENTOTIMECTRL1, 
] = [wx.NewId() for _init_ctrls in range(25)]

class Frame_Agendamento(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME_AGENDAMENTO,
              name=u'Frame_Agendamento', parent=prnt, pos=wx.Point(242, 52),
              size=wx.Size(1124, 682),
              style=wx.MAXIMIZE | wx.DEFAULT_FRAME_STYLE,
              title=u'Frame_Agendamento')
        self.SetClientSize(wx.Size(1116, 649))
        self.SetBackgroundColour(wx.Colour(255, 255, 255))
        self.Center(wx.BOTH)

        self.CristinasStaticBox = wx.StaticBox(id=wxID_FRAME_AGENDAMENTOCRISTINASSTATICBOX,
              label=u"Cristina's Hair - Agendamentos",
              name=u'CristinasStaticBox', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(1112, 640), style=0)
        self.CristinasStaticBox.Show(False)

        self.calendarCtrl1 = wx.calendar.CalendarCtrl(date=wx.DateTime.Now(),
              id=wxID_FRAME_AGENDAMENTOCALENDARCTRL1, name='calendarCtrl1',
              parent=self, pos=wx.Point(48, 24), size=wx.Size(176, 136),
              style=wx.calendar.CAL_SHOW_HOLIDAYS)
        self.calendarCtrl1.Bind(wx.calendar.EVT_CALENDAR_SEL_CHANGED,
              self.OnCalendarCtrl1CalendarSelChanged,
              id=wxID_FRAME_AGENDAMENTOCALENDARCTRL1)

        self.button1 = wx.Button(id=wxID_FRAME_AGENDAMENTOBUTTON1,
              label=u'Voltar', name='button1', parent=self, pos=wx.Point(56,
              512), size=wx.Size(136, 72), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME_AGENDAMENTOBUTTON1)

        self.button2 = wx.Button(id=wxID_FRAME_AGENDAMENTOBUTTON2,
              label=u'Novo Agendamento', name='button2', parent=self,
              pos=wx.Point(56, 224), size=wx.Size(136, 72), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button,
              id=wxID_FRAME_AGENDAMENTOBUTTON2)

        self.button3 = wx.Button(id=wxID_FRAME_AGENDAMENTOBUTTON3,
              label=u'Editar Agendamento', name='button3', parent=self,
              pos=wx.Point(56, 416), size=wx.Size(136, 72), style=0)
        self.button3.Enable(False)
        self.button3.Bind(wx.EVT_BUTTON, self.OnButton3Button,
              id=wxID_FRAME_AGENDAMENTOBUTTON3)

        self.button4 = wx.Button(id=wxID_FRAME_AGENDAMENTOBUTTON4,
              label=u'Excluir Agendamento', name='button4', parent=self,
              pos=wx.Point(56, 320), size=wx.Size(136, 72), style=0)
        self.button4.Enable(False)
        self.button4.Bind(wx.EVT_BUTTON, self.OnButton4Button,
              id=wxID_FRAME_AGENDAMENTOBUTTON4)

        self.staticText1 = wx.StaticText(id=wxID_FRAME_AGENDAMENTOSTATICTEXT1,
              label=u'Agenda Do Dia', name='staticText1', parent=self,
              pos=wx.Point(304, 16), size=wx.Size(140, 23), style=0)
        self.staticText1.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Tahoma'))

        self.staticText2 = wx.StaticText(id=wxID_FRAME_AGENDAMENTOSTATICTEXT2,
              label=u'Data:', name='staticText2', parent=self, pos=wx.Point(296,
              392), size=wx.Size(59, 25), style=0)
        self.staticText2.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Tahoma'))

        self.staticText3 = wx.StaticText(id=wxID_FRAME_AGENDAMENTOSTATICTEXT3,
              label=u'Cliente:', name='staticText3', parent=self,
              pos=wx.Point(288, 432), size=wx.Size(80, 25), style=0)
        self.staticText3.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Tahoma'))

        self.staticText4 = wx.StaticText(id=wxID_FRAME_AGENDAMENTOSTATICTEXT4,
              label=u'Horario:', name='staticText4', parent=self,
              pos=wx.Point(520, 392), size=wx.Size(87, 25), style=0)
        self.staticText4.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Tahoma'))

        self.staticText5 = wx.StaticText(id=wxID_FRAME_AGENDAMENTOSTATICTEXT5,
              label=u'Servi\xe7os:', name='staticText5', parent=self,
              pos=wx.Point(280, 480), size=wx.Size(95, 25), style=0)
        self.staticText5.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Tahoma'))

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME_AGENDAMENTOTEXTCTRL1,
              name='textCtrl1', parent=self, pos=wx.Point(376, 432),
              size=wx.Size(560, 29), style=0, value=u'')
        self.textCtrl1.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))
        self.textCtrl1.Enable(False)

        self.listBox1 = wx.ListBox(choices=[],
              id=wxID_FRAME_AGENDAMENTOLISTBOX1, name='listBox1', parent=self,
              pos=wx.Point(384, 480), size=wx.Size(208, 128), style=0)
        self.listBox1.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
              u'Tahoma'))
        self.listBox1.Bind(wx.EVT_LEFT_DCLICK, self.OnListBox1LeftDclick)

        self.datePickerCtrl1 = wx.DatePickerCtrl(id=wxID_FRAME_AGENDAMENTODATEPICKERCTRL1,
              name='datePickerCtrl1', parent=self, pos=wx.Point(368, 392),
              size=wx.Size(120, 29), style=wx.DP_SHOWCENTURY)
        self.datePickerCtrl1.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))
        self.datePickerCtrl1.Enable(True)
        self.datePickerCtrl1.SetLabel(u'')
        self.datePickerCtrl1.SetValue(wx.DateTimeFromDMY(21, 10, 2010, 0, 0, 0))
        self.datePickerCtrl1.Show(True)

        self.staticBox1 = wx.StaticBox(id=wxID_FRAME_AGENDAMENTOSTATICBOX1,
              label=u'Agendamento', name='staticBox1', parent=self,
              pos=wx.Point(264, 352), size=wx.Size(784, 280), style=0)
        self.staticBox1.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Tahoma'))

        self.timeCtrl1 = wx.lib.masked.timectrl.TimeCtrl(display_seconds=True,
              fmt24hr=False, id=wxID_FRAME_AGENDAMENTOTIMECTRL1,
              name='timeCtrl1', oob_color=wx.NamedColour('Yellow'), parent=self,
              pos=wx.Point(616, 392), size=wx.Size(58, 26), style=0,
              useFixedWidthFont=True, value='12:00:00 AM')
        self.timeCtrl1.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Courier New'))
        self.timeCtrl1.SetFormat('24HHMM')

        self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME_AGENDAMENTOTEXTCTRL2,
              name='textCtrl2', parent=self, pos=wx.Point(912, 392),
              size=wx.Size(100, 24), style=0, value=u'')
        self.textCtrl2.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))

        self.staticText6 = wx.StaticText(id=wxID_FRAME_AGENDAMENTOSTATICTEXT6,
              label=u'ID', name='staticText6', parent=self, pos=wx.Point(880,
              392), size=wx.Size(19, 19), style=0)
        self.staticText6.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Tahoma'))

        self.checkBox1 = wx.CheckBox(id=wxID_FRAME_AGENDAMENTOCHECKBOX1,
              label=u'Hoje', name='checkBox1', parent=self, pos=wx.Point(96,
              168), size=wx.Size(70, 13), style=0)
        self.checkBox1.SetValue(True)
        self.checkBox1.Bind(wx.EVT_CHECKBOX, self.OnCheckBox1Checkbox,
              id=wxID_FRAME_AGENDAMENTOCHECKBOX1)

        self.button5 = wx.Button(id=wxID_FRAME_AGENDAMENTOBUTTON5,
              label=u'Salvar Altera\xe7\xf5es', name='button5', parent=self,
              pos=wx.Point(872, 488), size=wx.Size(112, 48), style=0)
        self.button5.Enable(True)
        self.button5.Bind(wx.EVT_BUTTON, self.OnButton5Button,
              id=wxID_FRAME_AGENDAMENTOBUTTON5)

        self.listBox3 = wx.ListBox(choices=[],
              id=wxID_FRAME_AGENDAMENTOLISTBOX3, name='listBox3', parent=self,
              pos=wx.Point(624, 480), size=wx.Size(208, 128), style=0)
        self.listBox3.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
              u'Tahoma'))
        self.listBox3.Bind(wx.EVT_LEFT_DCLICK, self.OnListBox3LeftDclick)

        self.staticText7 = wx.StaticText(id=wxID_FRAME_AGENDAMENTOSTATICTEXT7,
              label=u'<', name='staticText7', parent=self, pos=wx.Point(600,
              536), size=wx.Size(16, 23), style=0)
        self.staticText7.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Tahoma'))

        self.button6 = wx.Button(id=wxID_FRAME_AGENDAMENTOBUTTON6,
              label=u'Cancelar Edi\xe7\xe3o', name='button6', parent=self,
              pos=wx.Point(872, 552), size=wx.Size(112, 48), style=0)
        self.button6.Bind(wx.EVT_BUTTON, self.OnButton6Button,
              id=wxID_FRAME_AGENDAMENTOBUTTON6)

        self.textCtrl3 = wx.lib.masked.textctrl.TextCtrl(id=wxID_FRAME_AGENDAMENTOTEXTCTRL3,
              name='textCtrl3', parent=self, pos=wx.Point(408, 120),
              size=wx.Size(100, 21), style=0, value=u'')
        self.textCtrl3.SetMask(u'###.###.###-##')
        self.textCtrl3.SetAutoformat('')
        self.textCtrl3.SetDatestyle('MDY')
        self.textCtrl3.SetFormatcodes('')
        self.textCtrl3.SetDescription('')
        self.textCtrl3.SetExcludeChars('')
        self.textCtrl3.SetValidRegex('')

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.staticText7.Show(False)
        self.textCtrl1.Show(False)
        self.datePickerCtrl1.Show(False)
        self.timeCtrl1.Show(False)
        self.listBox1.Show(False)
        self.listBox3.Show(False)
        self.textCtrl2.Show(False)
        self.button5.Enable(False)
        self.button6.Enable(False)
        
        self.calendarCtrl1.SetDate(wx.DateTime.Today())
        self.OnCalendarCtrl1CalendarSelChanged(self)
        
        calendarioDateString = str(self.calendarCtrl1.GetDate())
        
        mes = int(calendarioDateString[0:2])
        dia = int(calendarioDateString[3:5])
        ano = int(calendarioDateString[6:8])+2000
        
        calendarioTodayDate = datetime.date(ano, mes, dia)
        
        ca = ControleAgendamento()
        ca.verificarArquivo()
        
        self.listBox2.Clear()
        for agendamento in ca.getListaDeAgendamentos():
            if agendamento.GetData() == calendarioTodayDate:
                ShowString = str(agendamento.GetAgendamentoID())+' - '+str(agendamento.GetHorario())[0:5]+' - '+str(agendamento.GetCliente())
                self.listBox2.Append(ShowString)
    
    def recarregarAgendamentos(self):
        ca = ControleAgendamento()
        
        self.listBox2.Clear()
        ca.verificarArquivo()
        
        data = self.calendarCtrl1.GetDate()
        dia = data.GetDay()
        mes =  data.GetMonth()+1
        ano = data.GetYear()
        
        if data == wx.DateTime.Today():
            self.checkBox1.SetValue(True)
        else:
            self.checkBox1.SetValue(False)
        
        self.staticText1.SetLabel(('Agenda do Dia - %s/%s/%s')%(dia, mes, ano))
        
        
        calendarioDiaSelected = datetime.date(ano, mes, dia)
        
        lista = ca.getListaDeAgendamentos()
        for x in lista:
            if x.GetData() == calendarioDiaSelected:
                ShowString = str(x.GetAgendamentoID())+' - '+str(x.GetHorario())[0:5]+' - '+str(x.GetCliente())
                self.listBox2.Append(ShowString)
    
    def AtivarDesativarCampoEdicao(self, VouF):
        self.button5.Enable(VouF)
        self.listBox3.Show(VouF)
        self.staticText7.Show(VouF)
        self.timeCtrl1.Enable(VouF)
        self.datePickerCtrl1.Enable(VouF)
        self.listBox1.Enable(VouF)
        self.button6.Enable(VouF)
        

        
    def OnButton1Button(self, event):
        import MainFrame
        MainFrame.create(None).Show()
        self.Close()


    def OnButton2Button(self, event):
        
        tela = CriarAgendamento_Frame(None)
        tela.Show()
        self.Close()

    def OnCalendarCtrl1CalendarSelChanged(self, event):
        self.recarregarAgendamentos()

    def OnCheckBox1Checkbox(self, event):
        if self.checkBox1.GetValue() == True:
            self.calendarCtrl1.SetDate(wx.DateTime.Now())
            self.OnCalendarCtrl1CalendarSelChanged(self)
            self.CheckBox1.SetValue(True)

    def OnListBox2ListboxDclick(self, event):
        self.button3.Enable(True)
        self.button4.Enable(True)
        
        self.listBox3.Show(False)
        self.staticText7.Show(False)
        self.button5.Enable(False)
        
        self.listBox1.Clear()
        self.listBox3.Clear()
        
        cs = ControleServico()
        cs.verificarArquivo()
        ca = ControleAgendamento()
        ca.verificarArquivo()
        
        pos = self.listBox2.GetSelection()
        
        aux1 = self.listBox2.GetString(pos)
        aux2 = aux1[0:5]
        
        procurado = ca.procurarAgendamentoID(aux2)
        
        self.textCtrl2.Show(True)
        self.textCtrl2.SetValue(procurado.GetAgendamentoID())
        self.textCtrl2.Enable(False)       
        
        self.textCtrl1.Show(True)
        self.textCtrl1.SetValue(procurado.GetCliente())
        
        self.timeCtrl1.Show(True)
        horas = str(procurado.GetHorario())[0:5]
        self.timeCtrl1.SetValue(horas)
        
        self.listBox1.Show(True)
        for servico in procurado.GetServicosDoAgendamento():
            self.listBox1.Append(servico)
        
        for servicos in cs.getListaDeServicos():
            if servicos.GetNome() in procurado.GetServicosDoAgendamento():
                pass
            else:
                self.listBox3.Append(servicos.GetNome())
        
        dataString = str(procurado.GetData())
        ano = int(dataString[0:4])
        mes = int(dataString[5:7])-1
        dia = int(dataString[8:10])
        data = wx.DateTimeFromDMY(dia, mes, ano, 0, 0, 0)
        
        self.datePickerCtrl1.Show(True)
        self.datePickerCtrl1.SetValue(data)
        
        self.AtivarDesativarCampoEdicao(False)

    def OnButton3Button(self, event):
        self.AtivarDesativarCampoEdicao(True)
        self.button2.Enable(False)
        self.button4.Enable(False)
        
        
    def OnButton4Button(self, event):
        ca = ControleAgendamento()
        ca.excluirAgendamento(self.textCtrl2.GetValue())

        #recarregar lista de agendamentos do dia
        self.recarregarAgendamentos()
        
        wx.MessageBox('Agendamento Excluido com Sucesso', 'Agenda', wx.OK)
        
        self.staticText7.Show(False)
        self.textCtrl1.Show(False)
        self.datePickerCtrl1.Show(False)
        self.timeCtrl1.Show(False)
        self.listBox1.Show(False)
        self.listBox3.Show(False)
        self.textCtrl2.Show(False)
        self.button5.Enable(False)
        self.button6.Enable(False)
        
    #botao de salvar alteracoes
    def OnButton5Button(self, event):
        ca = ControleAgendamento()
        
        AgendamentoID = self.textCtrl2.GetValue()
        
        horarioStr = str(self.timeCtrl1.GetValue())
        hora = int(horarioStr[0:2])
        minutos = int(horarioStr[3:5])
        
        dataStr = str(self.datePickerCtrl1.GetValue())
        mes = int(dataStr[0:2])
        dia = int(dataStr[3:5])
        ano = int(dataStr[6:8])+2000
        
        Obj_Cliente = self.textCtrl1.GetValue()
        servicos = self.listBox1.GetItems()
        
        ca.alterarAgendamentoTodo(AgendamentoID, hora, minutos, dia, mes, ano, Obj_Cliente, servicos)
        self.AtivarDesativarCampoEdicao(False)

        self.button2.Enable(True)
        self.button4.Enable(True)

        
        #recarregar lista de agendamentos do dia
        self.recarregarAgendamentos()
        
        wx.MessageBox('Agendamento Alterado Com Sucesso', 'Agenda', wx.OK)
        
        
    #listbox dos itens que serao salvos
    def OnListBox1LeftDclick(self, event):
        pos = self.listBox1.GetSelection()
        nome = self.listBox1.GetString(pos)
        
        self.listBox3.Append(nome)
        self.listBox1.Delete(pos)

    #listbox dos itens disponiveis
    def OnListBox3LeftDclick(self, event):
        pos = self.listBox3.GetSelection()
        nome = self.listBox3.GetString(pos)
        
        self.listBox1.Append(nome)
        self.listBox3.Delete(pos)

    def OnButton6Button(self, event):
        self.AtivarDesativarCampoEdicao(False)
        self.button2.Enable(True)
        self.button4.Enable(True)

